﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Drawing;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using System.Data;

/// <summary>
/// Summary description for CoverPagePDFOutput
/// </summary>
public class CoverPagePDFOutput
{
    private string _refnumber;
    public string RefNumber
    {
        get { return _refnumber; }
        set { _refnumber = value; }
    }

    private string _customername;
    public string CustomerName
    {
        get { return _customername; }
        set { _customername = value; }
    }

    private string _mailName;
    public string MailName
    {
        get { return _mailName; }
        set { _mailName = value; }
    }

    private string _mailaddress;
    public string MailAddress
    {
        get { return _mailaddress; }
        set { _mailaddress = value; }
    }

    private string _maildistrict;
    public string MailDistrict
    {
        get { return _maildistrict; }
        set { _maildistrict = value; }
    }

    private string _mailsubdistrict;
    public string MailSubDistrict
    {
        get { return _mailsubdistrict; }
        set { _mailsubdistrict = value; }
    }

    private string _mailprovince;
    public string MailProvince
    {
        get { return _mailprovince; }
        set { _mailprovince = value; }
    }

    private string _mailpostcode;
    public string MailPostCode
    {
        get { return _mailpostcode; }
        set { _mailpostcode = value; }
    }

    private string _policyno;
    public string PolicyNo
    {
        get { return _policyno; }
        set { _policyno = value; }
    }

    private string _insurancecode;
    public string InsuranceCode
    {
        get { return _insurancecode; }
        set { _insurancecode = value; }
    }

    private string _licenseplate;
    public string LicensePlate
    {
        get { return _licenseplate; }
        set { _licenseplate = value; }
    }

    
    private DateTime dateTimeNow = CE.Global.General.GetUTCDateTime();

    public string GeneratePDF()
    {
        // Create a new PDF document
        PdfDocument document = new PdfDocument();
        // Set font encoding to unicode

        XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);

        document.Info.Title = "Created with PDFsharp";

        // Create an empty page
        PdfPage page = document.AddPage();

        // Get an XGraphics object for drawing
        XGraphics gfx = XGraphics.FromPdfPage(page);

       

        // Create a font
        XFont fontHeader = new XFont("Tahoma", 13, XFontStyle.Bold, options);
        XFont font = new XFont("Tahoma", 10d, XFontStyle.Regular, options);
        XFont fontFooter = new XFont("Tahoma", 11, XFontStyle.Bold, options);
        XFont fontAddress = new XFont("Tahoma", 12, XFontStyle.Regular, options);
        XColor _xcolor = new XColor();
        _xcolor.R = 0;
        _xcolor.G = 0;
        _xcolor.B = 0;
        
        int curX = 60;
        int curY = 90;

        if (_mailName.Trim() != "")
        {
            gfx.DrawString("คุณ" + _mailName, font, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        }
        else
        {
            gfx.DrawString("คุณ" + _customername, font, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        }
        curY += 15;

        if (_mailaddress.Trim().Length > 55)
        {
            string add1 = "", add2 = "";
            string[] splitSpace = _mailaddress.Trim().Split(' ');
            Int32 splitNum = splitSpace.Length / 2;
            for (int i = 0; i < splitSpace.Length; i++)
            {
                if (i < splitNum)
                {
                    add1 += splitSpace[i];
                }
                else
                {
                    add2 += splitSpace[i];
                }
            }
            gfx.DrawString(add1, font, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
            curY += 15;
            gfx.DrawString(add2, font, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
            curY += 15;
        }
        else
        {
            gfx.DrawString(_mailaddress, font, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
            curY += 15;
        }

        gfx.DrawString("ต." + _mailsubdistrict + " อ." + _maildistrict, font, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        curY += 15;

        gfx.DrawString(_mailprovince, font, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        curY += 15;

        gfx.DrawString(_mailpostcode, font, XBrushes.Black, new XRect(curX, curY, 50, 20), XStringFormat.TopLeft);
        curY += 50;


        gfx.DrawString("Ref: เลขที่กรมธรรม์. / บริษัทประกันภัย : " + _policyno + " / " + _insurancecode, font, XBrushes.Black, new XRect(curX, curY, 500, 20), XStringFormat.TopLeft);
        curY += 15;
        gfx.DrawString("                                   เลขทะเบียน : " + _licenseplate, font, XBrushes.Black, new XRect(curX, curY, 500, 20), XStringFormat.TopLeft);
        curY += 70;

        //gfx.DrawString(Resources.CarInsuranceResultPage.DateNew, font, XBrushes.Black, new XRect(curX + 300, curY, 30, 15), XStringFormat.TopLeft);
        //gfx.DrawString(":", font, XBrushes.Black, new XRect(curX + 400, curY, 50, 15), XStringFormat.TopLeft);
        //gfx.DrawString(dateTimeNow.ToString("dd-MMM-yyyy"), font, XBrushes.Black, new XRect(curX + 405, curY, 50, 15), XStringFormat.TopLeft);

        gfx.DrawString("เรียน  คุณ" + _customername, font, XBrushes.Black, new XRect(curX, curY, 200, 15), XStringFormat.TopLeft);
        curY += 50;

        gfx.DrawString("ขอขอบพระคุณท่านที่เลือกซื้อประกันภัยรถยนต์ของท่าน ผ่านทาง www.gluaygluay.com ทางเราหวังว่าท่านจะได้รับ", font, XBrushes.Black, new XRect(curX, curY, 700, 20), XStringFormat.TopLeft);
        curY += 15;
        gfx.DrawString("ความสุขกับบริการของเราและหวังเป็นอย่างยิ่งว่าจะได้ช่วยเหลือท่านตามที่ท่านต้องการ เราหวังว่าจะได้รับการสนับสนุน", font, XBrushes.Black, new XRect(curX, curY, 700, 20), XStringFormat.TopLeft);
        curY += 15;
        gfx.DrawString("จากท่านลูกค้าในการทำประกันภัยรถยนต์ต่อไป", font, XBrushes.Black, new XRect(curX, curY, 700, 20), XStringFormat.TopLeft);
        curY += 15;
        gfx.DrawString("    เราพร้อมที่จะให้ความช่วยเหลือท่าน ที่เบอร์  02-268 8222 ทุกวันจันทร์ถึงวันศุกร์ 09:00-18:00 หรือท่านสามารถ", font, XBrushes.Black, new XRect(curX, curY, 700, 20), XStringFormat.TopLeft);
        curY += 15;
        gfx.DrawString("ส่งอีเมล์ถึงเราได้ที่  support@gluaygluay.com", font, XBrushes.Black, new XRect(curX, curY, 700, 20), XStringFormat.TopLeft);
        curY += 80;

        gfx.DrawString("ขอแสดงความนับถือ ,", font, XBrushes.Black, new XRect(curX, curY, 200, 20), XStringFormat.TopLeft);
        curY += 40;
        gfx.DrawString("E: support@gluaygluay.com ", font, XBrushes.Black, new XRect(curX, curY, 200, 20), XStringFormat.TopLeft);
        curY += 20;
        gfx.DrawString("W: www.gluaygluay.com", font, XBrushes.Black, new XRect(curX, curY, 200, 20), XStringFormat.TopLeft);
        curY += 20;
        gfx.DrawString("หมายเหตุ : ติดต่อการเคลมประกันภัย โปรดดูเอกสาร Claim Hotlineที่แนบมาพร้อมกรมธรรม์", font, XBrushes.Black, new XRect(curX, curY, 200, 20), XStringFormat.TopLeft);
        curY += 20;

        string _filePath = GetFilePath();
        string filename = _filePath + _refnumber.Trim() + ".pdf"; //DateTime.Now.ToString("_ddMMMyyyy_hhmmss_tt")
        //document.Save(filename);
        // ...and start a viewer.
        //Response.Redirect(filename);
        System.IO.MemoryStream stream = new System.IO.MemoryStream();
        document.Save(filename);
        stream.Close();
        document.Close();
        return filename;
    }

    private string GetFilePath()
    {
        string _filePath = CE.Configuration.MyConfigurationManager.GetAppSettingValue("NonGGFileOutputPath").ToString();
        _filePath += "pdfoutput\\carinsurance\\CoverPage\\";
        CE.InputOut.MyIO _myIO = new CE.InputOut.MyIO();

        string _curYear = dateTimeNow.ToString("yyyy"); //DateTime.Now.ToString("yyyy");
        if (!_myIO.CheckDirectory(_filePath + _curYear))
        {
            if (_myIO.CreateDirectory(_filePath, _curYear))
            {
                _filePath += _curYear + "\\";
            }
        }
        else
        {
            _filePath += _curYear + "\\";
        }

        string _curMonth = dateTimeNow.ToString("MMMM"); //DateTime.Now.ToString("MMMM");
        if (!_myIO.CheckDirectory(_filePath + _curMonth))
        {
            if (_myIO.CreateDirectory(_filePath, _curMonth))
            {
                _filePath += _curMonth + "\\";
            }
        }
        else
        {
            _filePath += _curMonth + "\\";
        }

        string _todaydate = dateTimeNow.ToString("ddMMMMyyyy"); //DateTime.Now.ToString("ddMMMMyyyy");
        if (!_myIO.CheckDirectory(_filePath + _todaydate))
        {
            if (_myIO.CreateDirectory(_filePath, _todaydate))
            {
                _filePath += _todaydate + "\\";
            }
        }
        else
        {
            _filePath += _todaydate + "\\";
        }

        return _filePath;
    }
}
